﻿using Meiam.System.Interfaces;
using Meiam.System.Interfaces.IService;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Meiam.System.Model.Dto;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Meiam.System.Model;
using Microsoft.Extensions.Logging;
using System.IO;
using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;
using System.Xml.Linq;
using System.Reflection.Metadata;
using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup;
using Castle.Core.Resource;
using MailKit.Search;
using Mysqlx;

namespace Meiam.System.Hostd.Controllers.Gokin
{
    /// <summary>
    ///  组件CRM_签收台账列表创建
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class CRMCreateTZSQController : ControllerBase
    {
        private readonly ILogger _logger;
        public readonly IWMSTokenCheckService _WMSTokenCheckService;
        public readonly string con;
        public readonly string con_zh;
        public readonly string con_qh;
        public readonly string con_yb;
        public readonly string con_oa;


        public CRMCreateTZSQController(ILogger<CRMSignUploadRequestDto> logger, IWMSTokenCheckService wmsTokenCheckService, IConfiguration _configuration, ILoggerFactory loggerFactory)
        {
            _WMSTokenCheckService = wmsTokenCheckService;
            con = _configuration.GetConnectionString("DDM_DbConnection_ZH");
            con_zh = _configuration.GetConnectionString("WMSDbConnection_ZH");
            con_qh = _configuration.GetConnectionString("WMSDbConnection");
            con_yb = _configuration.GetConnectionString("WMSDbConnection_YB");
            con_oa = _configuration.GetConnectionString("OA_DbConnection");
            _logger = loggerFactory.CreateLogger<CRMCreateTZSQController>();
        }

        /// <summary>
        /// 组件CRM_签收台账列表创建
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostTZSQ([FromBody] CRMCreateTZSQRequestDto strparm)
        {
            string strMsg = "";
            StringBuilder strSql = new StringBuilder();
            List<string> strSqlList = new List<string>();
            Dictionary<string, string> dic = new Dictionary<string, string>(); //查询公用dic
            CRMCreateTZSQResponseDto re = new CRMCreateTZSQResponseDto();
            string re_id = "CR"+DateTimeOffset.Now.ToUnixTimeSeconds().ToString();

            try
            {
                if (strparm.source_no==""|| strparm.source_no==null)
                {
                    re.source_no = re_id;
                }
                else
                {
                    dic = new Dictionary<string, string>(); //查询公用dic
                    dic.Add("temp_id", strparm.source_no);
                    DataTable dt = MethodHelp.GetTableInfo("TMS0178", dic,con);
                    if (dt.Rows.Count>0)
                    {
                        re.data = DB.TableToListModel<CRMCreateTZSQResponseData>(dt);
                    }
                    else
                    {
                        strMsg = "临时TempID=<"+ strparm.source_no + ">找不到对应的签收台账记录";
                    }
                }

            }
            catch (Exception ex)
            {
                strMsg = ex.ToString();
            }

            if (strMsg == "")
            {
                re.code = 0;
                re.message = "";
            }
            else
            {
                re.code = 500;
                re.message = strMsg;

            }


            _logger.LogInformation(JsonConvert.SerializeObject(re));
            return new JsonResult(re);

        }


    }
}
